var app = getApp()
Component({
  data: {
    feiBox: "",
    hide_good_box: true,
  },
  properties: {},
  methods: {
    // 购物车动画-三次贝塞尔曲线
    dotMove(e){
      // 设置飞向购物车的图片
      this.setData({
          feiBox: e.currentTarget.dataset.item.shop_pic
      })
      // hide_good_box为true，good_box正在运动
      if (!this.data.hide_good_box) return;
      //当前点击位置的x，y坐标
      this.finger = {};
      var topPoint = {};
      this.finger['x'] = e.touches["0"].clientX;
      this.finger['y'] = e.touches["0"].clientY - 20;
      // 如果点击的位置小于购物车的位置，那么就让topPoint的y值等于点击的位置的y值，否则就让topPoint的y值等于购物车的y值
      if (this.finger['y'] < this.busPos['y']) {
          topPoint['y'] = this.finger['y'] - 150;
      } else {
          topPoint['y'] = this.busPos['y'] - 150;
      }
      topPoint['x'] = (this.finger['x'] + this.busPos['x']) / 2;
      this.linePos = app.bezier([this.finger, topPoint, this.busPos], 30);
      this.startAnimation(e);
    },
    //开始动画
    startAnimation: function(e) {
      var index = 0,
          that = this,
          bezier_points = that.linePos['bezier_points'];
      this.setData({
          hide_good_box: false,
          bus_x: that.finger['x'],
          bus_y: that.finger['y']
      })
      this.timer = setInterval(function() {
          index++;
          that.setData({
              bus_x: bezier_points[index]['x'],
              bus_y: bezier_points[index]['y']
          })
          if (index >= 28) {
              clearInterval(that.timer);
              that.setData({
                  hide_good_box: true,
                  hideCount: false,
                  count: that.data.count += 1
              })
          }
          
      }, 33);
      // 等动画执行完再执行加入购物车
      setTimeout(()=>{
          this.addShop(e)
      }
      ,1000)
    },
  },
  onLoad() {
    var that = this;
    //可视窗口x,y坐标
    this.busPos = {};
    console.log(app.globalData.ww);
    this.busPos['x'] = 45;
    this.busPos['y'] = app.globalData.hh ;
    this.dotMove(e)
  },
})